import 'dart:math';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../bean/bean.dart';
import 'dart:ui' as ui;

class BGPainter extends CustomPainter {
  List<CircleBean> list;
  final Paint _paint = Paint()..isAntiAlias = true;
  BGPainter({required this.list});
  @override
  void paint(Canvas canvas, Size size) {
    for (var element in list) {
      _paint.color = element.color;
      canvas.drawCircle(element.offset, element.radius, _paint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}

class AnimPainter extends CustomPainter {
  final Paint _paint = Paint()..isAntiAlias = true;
  Random random = Random();
  List list;
  double angle, radius, start;
  AnimPainter(
      {required this.list,
      required this.angle,
      required this.radius,
      required this.start});
  @override
  void paint(Canvas canvas, Size size) {
    var center = Offset(size.width / 2, size.height / 2);
    _paint.style = PaintingStyle.stroke;
    _paint.style = PaintingStyle.fill;
    drawButton(size, canvas, center);
    drawStart(size, canvas, center);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }

  void drawStart(Size size, Canvas canvas, Offset center) {
    if (start == 0) {
    } else {
      _paint.color = Colors.blue;
      canvas.drawCircle(center, 40, _paint);
      Paragraph paragraph =
          getParagraph("start", 17, size.width, textColor: Colors.yellow);
      canvas.drawParagraph(paragraph, Offset(0, size.height / 2 - 10));
    }
  }

  void drawButton(Size size, Canvas canvas, Offset center) {
    if (radius == 0 || angle == 10) {
    } else {
      _paint.color = Colors.orangeAccent;
      double i = angle;
      for (String element in list) {
        Offset pos = center + Offset.fromDirection(i, radius);
        canvas.drawCircle(pos, start + 20, _paint);
        Paragraph paragraph = getParagraph(element, start, 100);
        canvas.drawParagraph(paragraph, Offset(pos.dx - 50, pos.dy - 10));
        i = i + 11;
      }
    }
  }
}

Paragraph getParagraph(String text, double textSize, double itemWidth,
    {Color textColor = Colors.white}) {
  var pb = ParagraphBuilder(ParagraphStyle(
      fontWeight: FontWeight.normal,
      textAlign: TextAlign.center,
      fontSize: textSize))
    ..pushStyle(ui.TextStyle(color: textColor))
    ..addText(text);
  return pb.build()..layout(ParagraphConstraints(width: itemWidth));
}
